/**
 * 
 */
package com.dy.agent.agent.api.biz.impl;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.joda.time.DateTime;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;
import org.springframework.stereotype.Service;

import com.dy.agent.agent.api.biz.AgentAnchorBiz;
import com.dy.agent.agent.api.model.dto.StatisticsQueryCondition;
import com.dy.agent.agent.api.model.entiry.AgentAnchorStatistics;
import com.dy.agent.agent.api.model.entiry.AgentDailyView;
import com.dy.agent.agent.api.model.vo.AnchorDailyIncomeView;
import com.dy.agent.agent.api.model.vo.AnchorMonthIncomeView;
import com.dy.agent.agent.api.model.vo.AnchorPlayLengthView;
import com.dy.agent.agent.api.service.AgentAnchorStatisticsService;
import com.dy.agent.agent.api.service.AgentDailyViewService;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;

/**
 * @author leiruiqi
 *
 */
@Service
public class AgentAnchorBizImpl implements AgentAnchorBiz {

	@Resource
	private AgentAnchorStatisticsService agentAnchorStatisticsService;

	@Resource
	private AgentDailyViewService agentDailyViewService;

	public void buildCurrentAgentView(String viewDateStr) {
		DateTimeFormatter dayformat = DateTimeFormat.forPattern("yyyyMMdd");
		DateTimeFormatter monthformat = DateTimeFormat.forPattern("yyyyMM");
		// 默认是当天自然日
		if (viewDateStr == null) {
			viewDateStr = DateTime.now().toString(dayformat);
		}

		DateTime dateTime = DateTime.parse(viewDateStr, dayformat);

		String lastMonthStr = dateTime.plusMonths(-1).toString(monthformat);

		StatisticsQueryCondition condition = new StatisticsQueryCondition();
		condition.setStartDt(lastMonthStr);

		// 用大于上月起始日期的方式查询最近两个月的记录
		List<AgentAnchorStatistics> last2monthDailyStatisticsList = agentAnchorStatisticsService
				.queryListByCondition(condition);

		Map<Long, AgentDailyView> map = buildlast2monthView(last2monthDailyStatisticsList, viewDateStr);
		// 上一日日期
		String last1DayStr = dateTime.plusDays(-1).toString(dayformat);
		agentDailyViewService.delByIdsAndDt(map.keySet(), last1DayStr);

		agentDailyViewService.batchCreate(map.values());

	}

	public Map<Long, AgentDailyView> buildlast2monthView(List<AgentAnchorStatistics> last2monthDailyStatisticsList,
			String todayDt) {

		Map<Long, AgentDailyView> agentCurrentViewMap = Maps.newHashMap();
		DateTimeFormatter dayformat = DateTimeFormat.forPattern("yyyyMMdd");

		DateTimeFormatter monthformat = DateTimeFormat.forPattern("yyyyMM");
		// 默认是当天自然日
		if (todayDt == null) {
			todayDt = DateTime.now().toString(dayformat);
		}

		DateTime dateTime = DateTime.parse(todayDt, dayformat);

		// 本月月份
		String currentMonthStr = dateTime.toString(monthformat);
		// 上个月月份
		String lastMonthStr = dateTime.plusMonths(-1).toString(monthformat);

		// 上一日日期
		String last1DayStr = dateTime.plusDays(-1).toString(dayformat);
		// 记dt为昨日生效数据

		String last7DayStr = dateTime.plusDays(-7).toString(dayformat);

		int last1Day = Integer.parseInt(last1DayStr);
		int last7Day = Integer.parseInt(last7DayStr);

		for (AgentAnchorStatistics statistics : last2monthDailyStatisticsList) {
			Long tempGid = statistics.getGid();
			if (tempGid == null) {
				continue;
			}
			AgentDailyView view = agentCurrentViewMap.get(tempGid);
			if (view == null) {
				view = new AgentDailyView(tempGid, last1DayStr);
				agentCurrentViewMap.put(tempGid, view);
			}
			Long realIncome = statistics.getRealIncome();
			if (realIncome == null) {
				realIncome = 0L;
			}
			Long chouqinIncome = statistics.getChouqinIncome();
			if (chouqinIncome == null) {
				chouqinIncome = 0L;
			}
			Long chouqinEstimate = statistics.getChouqinEstimate();
			if (chouqinEstimate == null) {
				chouqinEstimate = 0L;
			}
			Long playLength = statistics.getPlayLength();
			if (playLength == null) {
				playLength = 0L;
			}
			Long tempEstimateIncome = realIncome - chouqinIncome + chouqinEstimate;

			int tempDtInt = Integer.parseInt(statistics.getDt());
			if (tempDtInt == last1Day) {

				view.setDay1Income(view.getDay1Income() + tempEstimateIncome);
				view.setDay1PlayLength(view.getDay1PlayLength() + playLength);
			}
			if (tempDtInt <= last1Day && tempDtInt >= last7Day) {

				view.setDay7Income(view.getDay7Income() + tempEstimateIncome);
				view.setDay7PlayLength(view.getDay7PlayLength() + playLength);
			}
			// 本月数据
			if (StringUtils.compare(statistics.getDt(), currentMonthStr) > 0) {

				view.setMonthIncome(view.getMonthIncome() + tempEstimateIncome);
				view.setMonthPlayLength(view.getMonthPlayLength() + playLength);

			}
			// 上月数据
			else if (StringUtils.compare(statistics.getDt(), lastMonthStr) > 0) {

				view.setLastMonthIncome(view.getLastMonthIncome() + realIncome);
				view.setLastMonthPlayLength(view.getLastMonthPlayLength() + playLength);
			}

		}

		return agentCurrentViewMap;
	}

	@Override
	public List<AnchorDailyIncomeView> converRecord2AnchorIncomeView(List<AgentAnchorStatistics> list) {

		List<AnchorDailyIncomeView> viewList = Lists.newArrayList();
		for (AgentAnchorStatistics statistics : list) {
			AnchorDailyIncomeView view = new AnchorDailyIncomeView();
			view.setGid(statistics.getGid());
			view.setUid(statistics.getUid());
			view.setDt(statistics.getDt());

			Long realIncome = statistics.getRealIncome();
			if (realIncome == null) {
				realIncome = 0L;
			}
			Long chouqinIncome = statistics.getChouqinIncome();
			if (chouqinIncome == null) {
				chouqinIncome = 0L;
			}
			Long chouqinEstimate = statistics.getChouqinEstimate();
			if (chouqinEstimate == null) {
				chouqinEstimate = 0L;
			}
			Long playLength = statistics.getPlayLength();
			if (playLength == null) {
				playLength = 0L;
			}
			// 酬勤需要估值，用实际收入-实际酬勤+预计酬勤
			Long tempEstimateIncome = realIncome - chouqinIncome + chouqinEstimate;
			view.setIncomeEstimate(tempEstimateIncome);

			viewList.add(view);
		}
		return viewList;
	}

	public Map<Long, AnchorPlayLengthView> buildAnchorLast2Month4PlayLength(Long gid, String todayDt) {
		Map<Long, AnchorPlayLengthView> agentCurrentViewMap = Maps.newHashMap();
		DateTimeFormatter dayformat = DateTimeFormat.forPattern("yyyyMMdd");

		DateTimeFormatter monthformat = DateTimeFormat.forPattern("yyyyMM");
		// 默认是当天自然日
		if (todayDt == null) {
			todayDt = DateTime.now().toString(dayformat);
		}

		DateTime dateTime = DateTime.parse(todayDt, dayformat);

		// 本月月份
		String currentMonthStr = dateTime.toString(monthformat);
		// 上个月月份
		String lastMonthStr = dateTime.plusMonths(-1).toString(monthformat);

		StatisticsQueryCondition condition = new StatisticsQueryCondition();
		condition.setStartDt(lastMonthStr);
		condition.setGid(gid);

		// 用大于上月起始日期的方式查询最近两个月的记录
		List<AgentAnchorStatistics> last2monthDailyStatisticsList = agentAnchorStatisticsService
				.queryListByCondition(condition);

		// 上一日日期
		String last1DayStr = dateTime.plusDays(-1).toString(dayformat);
		// 记dt为昨日生效数据

		String last7DayStr = dateTime.plusDays(-7).toString(dayformat);

		int last1Day = Integer.parseInt(last1DayStr);
		int last7Day = Integer.parseInt(last7DayStr);

		for (AgentAnchorStatistics statistics : last2monthDailyStatisticsList) {
			Long tempUid = statistics.getUid();
			if (tempUid == null) {
				continue;
			}
			AnchorPlayLengthView view = agentCurrentViewMap.get(tempUid);
			if (view == null) {
				view = new AnchorPlayLengthView();
				view.setUid(statistics.getUid());
				view.setGid(statistics.getGid());
				view.setDt(statistics.getDt());
				view.setDay1PlayLength(0L);
				view.setDay7PlayLength(0L);
				view.setMonthPlayLength(0L);
				view.setLastMonthPlayLength(0L);
				agentCurrentViewMap.put(tempUid, view);
			}
			Long realIncome = statistics.getRealIncome();
			if (realIncome == null) {
				realIncome = 0L;
			}
			Long chouqinIncome = statistics.getChouqinIncome();
			if (chouqinIncome == null) {
				chouqinIncome = 0L;
			}
			Long chouqinEstimate = statistics.getChouqinEstimate();
			if (chouqinEstimate == null) {
				chouqinEstimate = 0L;
			}
			Long playLength = statistics.getPlayLength();
			if (playLength == null) {
				playLength = 0L;
			}
			// Long tempEstimateIncome = realIncome - chouqinIncome +
			// chouqinEstimate;

			int tempDtInt = Integer.parseInt(statistics.getDt());
			if (tempDtInt == last1Day) {

				// view.setDay1Income(view.getDay1Income() +
				// tempEstimateIncome);
				view.setDay1PlayLength(view.getDay1PlayLength() + playLength);
			}
			if (tempDtInt <= last1Day && tempDtInt >= last7Day) {

				// view.setDay7Income(view.getDay7Income() +
				// tempEstimateIncome);
				view.setDay7PlayLength(view.getDay7PlayLength() + playLength);
			}
			// 本月数据
			if (StringUtils.compare(statistics.getDt(), currentMonthStr) > 0) {

				// view.setMonthIncome(view.getMonthIncome() +
				// tempEstimateIncome);
				view.setMonthPlayLength(view.getMonthPlayLength() + playLength);

			}
			// 上月数据
			else if (StringUtils.compare(statistics.getDt(), lastMonthStr) > 0) {

				// view.setLastMonthIncome(view.getLastMonthIncome() +
				// realIncome);
				view.setLastMonthPlayLength(view.getLastMonthPlayLength() + playLength);
			}

		}

		return agentCurrentViewMap;
	}

	public Map<Long, AnchorMonthIncomeView> listAgentAnchorMonthIncome(Long gid, String monthDt) {
		Map<Long, AnchorMonthIncomeView> map = Maps.newHashMap();
		DateTimeFormatter monthformat = DateTimeFormat.forPattern("yyyyMM");
		String nowMonthStr = DateTime.now().toString(monthformat);
		boolean isCurrentMonth = false;
		if (StringUtils.equals(nowMonthStr, monthDt)) {
			isCurrentMonth = true;
		}
		DateTime monthDateTime = DateTime.parse(monthDt, monthformat);

		String nextMonthStr = monthDateTime.plusMonths(1).toString(monthformat);

		StatisticsQueryCondition condition = new StatisticsQueryCondition();
		condition.setGid(gid);
		condition.setStartDt(monthDt);
		condition.setEndDt(nextMonthStr);
		condition.setCompaDtEqual(false);

		List<AgentAnchorStatistics> list = agentAnchorStatisticsService.queryListByCondition(condition);
		for (AgentAnchorStatistics statistics : list) {
			Long uid = statistics.getUid();
			AnchorMonthIncomeView view = map.get(uid);
			if (view == null) {
				view = new AnchorMonthIncomeView();
				view.setGid(gid);
				view.setUid(uid);
				view.setDt(monthDt);
				view.setIncome(0L);
				if (isCurrentMonth) {
					Long tempIncome = statistics.getRealIncome() - statistics.getChouqinIncome()
							+ statistics.getChouqinEstimate();
					view.setIncome(view.getIncome() + tempIncome);
				} else {
					view.setIncome(view.getIncome() + statistics.getRealIncome());
				}

			}
		}
		return map;
	}
}
